package com.duoketech.controller.web;

import com.duoketech.controller.BaseController;
import com.duoketech.exception.ValidateException;
import com.duoketech.interceptor.MemberInterceptor;
import com.duoketech.model.member.Member;
import com.duoketech.service.web.member.MemberService;
import com.duoketech.utils.DbUtil;
import com.duoketech.utils.upload.QiNiuUpload;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.upload.UploadFile;
import com.qiniu.common.QiniuException;

import net.coobird.thumbnailator.Thumbnails;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@Clear
public class TravelController extends BaseController {

	public void login(){
		render(travelView("login.html"));
	}
	@Before(MemberInterceptor.class)
	public void index(){
		render(travelView("index.html"));
	}

	public void doLogin(){
		String userName = getPara("userName","");
		String userPass = getPara("userPass","");
        try {
            Member member = MemberService.ME.doLogin(userName,userPass);
            setCookie(COOKIE_SHOP_NAME,member.getInt(Member.ID)+"",COOKIE_TIME_OUT_1_MONTH);
            setMemberShop(member);
            renderSuccess("登录成功...");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
	}

	public void logout(){
		removerMemberShop();
		redirect("/travel/login");
	}

	public void upload(){
		UploadFile uploader = getFile();
		try {
			File file=  uploader.getFile();
			Thumbnails.of(file).scale(1).outputQuality(0.85).toFile(file);
			String url = QiNiuUpload.uploadByJfinal(new FileInputStream(file),"pc-");
			file.delete();
			renderSuccess(url);
		} catch (QiniuException e) {
			renderError("上传报错");
		} catch (FileNotFoundException e) {
			renderError("文件找不到");
		} catch (IOException e) {
			renderError("压缩出错");
		}
	}

	public void uploadForKindEditor() {
		UploadFile upfile=getFile();
		String category = getPara("category","pc-");
		try {
			File file=  upfile.getFile();
			Thumbnails.of(file).scale(1).outputQuality(0.85).toFile(file);
			String upload = QiNiuUpload.uploadByJfinal(new
					FileInputStream(file), category);
			file.delete();
			renderKindEditorSuccess(upload);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void map(){
		render("travel/map/index.html");
	}
}